package com.zhijiaoyun.controller;

import com.zhijiaoyun.common.CommonUtils;
import com.zhijiaoyun.common.DsConstant;
import com.zhijiaoyun.common.FileUtils;
import com.zhijiaoyun.common.StringUtils;
import com.zhijiaoyun.controller.base.BaseController;
import com.zhijiaoyun.log.SystemLog;
import com.zhijiaoyun.model.*;
import com.zhijiaoyun.model.dto.PageList;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author shisfish
 * @date 2017/6/6
 */
@Controller
@RequestMapping(value = "/setting", produces = "application/json; charset=utf-8")
public class SettingController extends BaseController {

    /**
     * 添加管理员
     *
     * @param account
     * @param username
     * @param password
     * @param authorityId
     * @param status
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/add-admin", method = RequestMethod.POST)
    @SystemLog(function = "addAdmin", description = "添加管理员")
    public Map<String, Object> addAdmin(String account, String username, String password, Long authorityId,
                                        Integer status, HttpServletRequest request) {
        String createUser = String.valueOf(request.getAttribute("createUser"));
        return adminService.addAdmin(account, username, password, authorityId, status, createUser);
    }

    // 修改
    @ResponseBody
    @RequestMapping(value = "/{adminId}/modifyAdmin", method = RequestMethod.POST)
    @SystemLog(function = "modifyAdmin", description = "修改管理员信息")
    public Map<String, Object> modifyAdmin(@PathVariable("adminId") Long adminId, String username, String password,
                                           Long authorityId, Integer status, HttpServletRequest request) {

        Map<String, Object> map = new HashMap<>();


        String createUser = String.valueOf(request.getAttribute("createUser"));

        Admin admin = adminService.getById(adminId);
        if (admin == null) {
            map.put(DsConstant.CODE, DsConstant.CODE_USER_LOST);
            map.put(DsConstant.MESSAGE, DsConstant.MESSAGE_USER_LOST);
            return map;
        }
        if (!StringUtils.isEmptyString(username)) {
            admin.setUsername(username);
        }

        if (!StringUtils.isEmptyString(password)) {
            admin.setPassword(CommonUtils.MD5(password));
        }

        if (authorityId != null) {
            admin.setAuthorityId(authorityId);
        }

        if (status != null) {
            admin.setStatus(status);
        }

        admin.setModifyUser(createUser);
        admin.setModifyTime(new Date());

        adminService.update(admin);
        map.put(DsConstant.CODE, DsConstant.CODE_SUCCESS);
        return map;
    }

    @ResponseBody
    @RequestMapping(value = "/admin", method = RequestMethod.GET)
    public Map<String, Object> adminList(int pageIndex, int pageSize, String account, String username, Integer status) {
        if (pageIndex <= 0) {
            pageIndex = 1;
        }
        if (pageSize <= 0) {
            pageSize = DsConstant.PAGESIZE_DEFAULT;
        }
        return adminService.page(pageIndex, pageSize, account, username, status);
    }

    /**
     * 管理员详情
     *
     * @param adminId
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/{adminId}/adminDetail", method = RequestMethod.GET)
    public Map<String, Object> adminDetail(@PathVariable("adminId") long adminId) {
        return adminService.getAdminById(adminId);
    }

    @ResponseBody
    @RequestMapping(value = "/{adminId}/deleteAdmin", method = RequestMethod.POST)
    @SystemLog(function = "deleteAdmin", description = "删除管理员")
    public Map<String, Object> deleteAdmin(@PathVariable("adminId") Long adminId) {

        Map<String, Object> map = new HashMap<>();
        adminService.deleteById(adminId);
        map.put(DsConstant.CODE, DsConstant.CODE_SUCCESS);

        return map;
    }

    @ResponseBody
    @RequestMapping(value = "/websitLink", method = RequestMethod.POST)
    public Map<String, Object> addWebsitLink(String name, String linkUrl, Integer sort, Integer status, HttpServletRequest request) {
        Map<String, Object> map = new HashMap<>();
        map.put(DsConstant.CODE, 1);
        String createUser = String.valueOf(request.getAttribute("createUser"));
        WebsiteLink websiteLink = websiteLinkService.getWebsiteLinkByName(name);
        if (websiteLink != null) {
            map.put(DsConstant.MESSAGE, "网站名已存在");
            return map;
        }

        websiteLink = websiteLinkService.getWebsiteLinkByLinkUrl(linkUrl);
        if (websiteLink != null) {
            map.put(DsConstant.MESSAGE, "网站地址已存在");
            return map;
        }

        websiteLink = new WebsiteLink(name, linkUrl, sort, status, createUser);

        websiteLinkService.addWebsiteLink(websiteLink);
        map.put(DsConstant.CODE, DsConstant.CODE_SUCCESS);
        return map;
    }

    @ResponseBody
    @RequestMapping(value = "/{linkId}/modifyWebsiteLink", method = RequestMethod.POST)
    public Map<String, Object> modifyCategory(@PathVariable("linkId") Long linkId, String name, String linkUrl,
                                              Integer sort, Integer status, HttpServletRequest request) {
        Map<String, Object> map = new HashMap<>();
        map.put(DsConstant.CODE, 1);
        String createUser = String.valueOf(request.getAttribute("createUser"));
        WebsiteLink websiteLink = websiteLinkService.getWebsiteLinkByLinkId(linkId);
        if (websiteLink == null) {
            map.put(DsConstant.MESSAGE, "友好网站不存在");
            return map;
        }

        if (!StringUtils.isEmptyString(name)) {
            WebsiteLink temp = websiteLinkService.getWebsiteLinkByNameNotLinkId(linkId, name);
            if (temp != null) {
                map.put(DsConstant.MESSAGE, "网站名已存在");
                return map;
            }

            websiteLink.setName(name);
        }

        if (!StringUtils.isEmptyString(linkUrl)) {
            WebsiteLink temp = websiteLinkService.getWebsiteLinkByLinkUrlNotLinkId(linkId, linkUrl);
            if (temp != null) {
                map.put(DsConstant.MESSAGE, "网站地址已存在");
                return map;
            }

            websiteLink.setLinkUrl(linkUrl);
        }

        if (sort != null) {
            websiteLink.setSort(sort);
        }

        if (status != null) {
            websiteLink.setStatus(status);
        }
        websiteLink.setModifyTime(new Date());
        websiteLink.setModifyUser(createUser);
        websiteLinkService.modifyWebsiteLink(websiteLink);
        map.put(DsConstant.CODE, DsConstant.CODE_SUCCESS);
        return map;
    }

    @ResponseBody
    @RequestMapping(value = "/{linkId}/linkStatusChange", method = RequestMethod.POST)
    public Map<String, Object> linkStatusChange(@PathVariable("linkId") Long linkId, Integer status, String token) {
        Map<String, Object> map = new HashMap<>();
        map.put(DsConstant.CODE, 1);
        Long lognId = CommonUtils.getAdminIdByToken(token);
        if (lognId == 0) {
            map.put(DsConstant.MESSAGE, "token过期");
            return map;
        }
        WebsiteLink websiteLink = websiteLinkService.getWebsiteLinkByLinkId(linkId);
        if (websiteLink == null) {
            map.put(DsConstant.MESSAGE, "友好网站不存在");
            return map;
        }

        websiteLink.setStatus(status);
        websiteLinkService.modifyWebsiteLink(websiteLink);
        map.put(DsConstant.CODE, DsConstant.CODE_SUCCESS);
        return map;
    }

    @ResponseBody
    @RequestMapping(value = "/websiteLinkList", method = RequestMethod.GET)
    public Map<String, Object> websiteLinkList(Integer pageIndex, String name, String linkUrl) {
        Map<String, Object> map = new HashMap<>();
        map.put(DsConstant.CODE, 1);
        PageList<WebsiteLink> websiteLinks = websiteLinkService.pageByParams(pageIndex, name, linkUrl);
        map.put("list", websiteLinks.getList());
        map.put("page", websiteLinks.getPageData());
        map.put(DsConstant.CODE, DsConstant.CODE_SUCCESS);
        return map;
    }

    @ResponseBody
    @RequestMapping(value = "{linkId}/deleteWebsiteLink", method = RequestMethod.POST)
    public Map<String, Object> deleteWebsiteLink(@PathVariable("linkId") Long linkId) {
        Map<String, Object> map = new HashMap<>();
        map.put(DsConstant.CODE, 1);
        WebsiteLink websiteLink = websiteLinkService.getWebsiteLinkByLinkId(linkId);
        if (websiteLink == null) {
            map.put(DsConstant.MESSAGE, "友好网站不存在");
            return map;
        }

        websiteLinkService.deleteWebsiteLink(websiteLink);
        map.put(DsConstant.CODE, DsConstant.CODE_SUCCESS);
        return map;
    }

    @ResponseBody
    @RequestMapping(value = "/addAuthority", method = RequestMethod.POST)
    public Map<String, Object> addAuthority(String name, String moduleIds, HttpServletRequest request) {
        Map<String, Object> map = new HashMap<>();
        map.put(DsConstant.CODE, 1);
        String createUser = String.valueOf(request.getAttribute("createUser"));
        Authority authority = authorityService.getAuthorityByName(name);
        if (authority != null) {
            map.put(DsConstant.MESSAGE, "权限名已存在");
            return map;
        }

        authority = new Authority(name, moduleIds, createUser);

        authorityService.addAuthority(authority);
        map.put(DsConstant.CODE, DsConstant.CODE_SUCCESS);
        return map;
    }

    @ResponseBody
    @RequestMapping(value = "/{authorityId}/modifyAuthority", method = RequestMethod.POST)
    public Map<String, Object> modifyAuthority(@PathVariable("authorityId") Long authorityId, String name,
                                               String moduleIds, HttpServletRequest request) {
        Map<String, Object> map = new HashMap<>();
        map.put(DsConstant.CODE, 1);
        String createUser = String.valueOf(request.getAttribute("createUser"));
        Authority authority = authorityService.getAuthorityByAuthorityId(authorityId);
        if (authority == null) {
            map.put(DsConstant.MESSAGE, "权限不存在");
            return map;
        }

        if (!StringUtils.isEmptyString(name)) {
            Authority temp = authorityService.getAuthorityByNameNotAuthorityId(authorityId, name);
            if (temp != null) {
                map.put(DsConstant.MESSAGE, "权限名已存在");
                return map;
            }

            authority.setName(name);
        }

        if (!StringUtils.isEmptyString(moduleIds)) {

            authority.setModuleIds(moduleIds);
        }

        authority.setModifyUser(createUser);
        authority.setModifyTime(new Date());
        authorityService.modifyAuthority(authority);
        map.put(DsConstant.CODE, DsConstant.CODE_SUCCESS);
        return map;
    }

    @ResponseBody
    @RequestMapping(value = "/authorityList", method = RequestMethod.GET)
    public Map<String, Object> authorityList(String name, String token) {
        Map<String, Object> map = new HashMap<>();
        map.put(DsConstant.CODE, 1);
        Long lognId = CommonUtils.getAdminIdByToken(token);
        if (lognId == 0) {
            map.put(DsConstant.MESSAGE, "token过期");
            return map;
        }
        List<Authority> authoritys = authorityService.getByParams(name);
        map.put("list", authoritys);
        map.put(DsConstant.CODE, DsConstant.CODE_SUCCESS);
        return map;
    }

    @ResponseBody
    @RequestMapping(value = "/addFastNavigation", method = RequestMethod.POST)
    public Map<String, Object> addFastNavigation(String name, String linkUrl, Integer sort, Integer status,
                                                 HttpServletRequest request) {
        Map<String, Object> map = new HashMap<>();
        map.put(DsConstant.CODE, 1);
        String createUser = String.valueOf(request.getAttribute("createUser"));
        FastNavigation fastNavigation = fastNavigationService.getFastNavigationByName(name);
        if (fastNavigation != null) {
            map.put(DsConstant.MESSAGE, "快速导航名已存在");
            return map;
        }

        fastNavigation = fastNavigationService.getFastNavigationByLinkUrl(linkUrl);
        if (fastNavigation != null) {
            map.put(DsConstant.MESSAGE, "网站地址已存在");
            return map;
        }

        fastNavigation = new FastNavigation(name, linkUrl, sort, status, createUser);

        fastNavigationService.addFastNavigation(fastNavigation);
        map.put(DsConstant.CODE, DsConstant.CODE_SUCCESS);
        return map;
    }

    @ResponseBody
    @RequestMapping(value = "/{navigationId}/modifyFastNavigation", method = RequestMethod.POST)
    public Map<String, Object> modifyFastNavigation(@PathVariable("navigationId") Long navigationId, String name,
                                                    String linkUrl, Integer sort, Integer status, HttpServletRequest request) {
        Map<String, Object> map = new HashMap<>();
        map.put(DsConstant.CODE, 1);
        String createUser = String.valueOf(request.getAttribute("createUser"));
        FastNavigation fastNavigation = fastNavigationService.getFastNavigationByNavigationId(navigationId);
        if (fastNavigation == null) {
            map.put(DsConstant.MESSAGE, "快速导航不存在");
            return map;
        }

        if (!StringUtils.isEmptyString(name)) {
            FastNavigation temp = fastNavigationService.getNavigationByNameNotNavigationId(navigationId, name);
            if (temp != null) {
                map.put(DsConstant.MESSAGE, "快速导航名已存在");
                return map;
            }

            fastNavigation.setName(name);
        }

        if (!StringUtils.isEmptyString(linkUrl)) {
            FastNavigation temp = fastNavigationService.getNavigationByLinkUrlNotNavigationId(navigationId, linkUrl);
            if (temp != null) {
                map.put(DsConstant.MESSAGE, "网站地址已存在");
                return map;
            }

            fastNavigation.setLinkUrl(linkUrl);
        }

        if (sort != null) {
            fastNavigation.setSort(sort);
        }

        if (status != null) {
            fastNavigation.setStatus(status);
        }
        fastNavigation.setModifyTime(new Date());
        fastNavigation.setModifyUser(createUser);
        fastNavigationService.modifyFastNavigation(fastNavigation);
        map.put(DsConstant.CODE, DsConstant.CODE_SUCCESS);
        return map;
    }

    @ResponseBody
    @RequestMapping(value = "{navigationId}/navigationStatusChange", method = RequestMethod.POST)
    public Map<String, Object> navigationStatusChange(@PathVariable("navigationId") Long navigationId, Integer status) {
        Map<String, Object> map = new HashMap<>();
        map.put(DsConstant.CODE, 1);
        FastNavigation fastNavigation = fastNavigationService.getFastNavigationByNavigationId(navigationId);
        if (fastNavigation == null) {
            map.put(DsConstant.MESSAGE, "友好网站不存在");
            return map;
        }

        fastNavigation.setStatus(status);
        fastNavigationService.modifyFastNavigation(fastNavigation);
        map.put(DsConstant.CODE, DsConstant.CODE_SUCCESS);
        return map;
    }

    @ResponseBody
    @RequestMapping(value = "/fastNavigationList", method = RequestMethod.GET)
    public Map<String, Object> fastNavigationList(Integer pageIndex, String name, String linkUrl) {
        Map<String, Object> map = new HashMap<>();
        map.put(DsConstant.CODE, 1);
        PageList<FastNavigation> fastNavigations = fastNavigationService.pageByParams(pageIndex, name, linkUrl);
        map.put("list", fastNavigations.getList());
        map.put("page", fastNavigations.getPageData());
        map.put(DsConstant.CODE, DsConstant.CODE_SUCCESS);
        return map;
    }

    @ResponseBody
    @RequestMapping(value = "{navigationId}/deleteFastNavigation", method = RequestMethod.POST)
    public Map<String, Object> deleteFastNavigation(@PathVariable("navigationId") Long navigationId) {
        Map<String, Object> map = new HashMap<>();
        map.put(DsConstant.CODE, 1);
        FastNavigation fastNavigation = fastNavigationService.getFastNavigationByNavigationId(navigationId);
        if (fastNavigation == null) {
            map.put(DsConstant.MESSAGE, "快速导航不存在");
            return map;
        }

        fastNavigationService.deleteFastNavigation(fastNavigation);
        map.put(DsConstant.CODE, DsConstant.CODE_SUCCESS);
        return map;
    }

    @ResponseBody
    @RequestMapping(value = "/addHelpCenter", method = RequestMethod.POST)
    public Map<String, Object> addHelpCenter(String problem, String answer, Integer sort, Integer status,
                                             HttpServletRequest request) {
        Map<String, Object> map = new HashMap<>();
        map.put(DsConstant.CODE, 1);
        String createUser = String.valueOf(request.getAttribute("createUser"));
        HelpCenter helpCenter = new HelpCenter(problem, answer, sort, status, createUser);

        helpCenterService.addHelpCenter(helpCenter);
        map.put(DsConstant.CODE, DsConstant.CODE_SUCCESS);
        return map;
    }

    @ResponseBody
    @RequestMapping(value = "/{helpId}/modifyHelpCenter", method = RequestMethod.POST)
    public Map<String, Object> modifyHelpCenter(@PathVariable("helpId") Long helpId, String problem, String answer,
                                                Integer sort, Integer status, HttpServletRequest request) {
        Map<String, Object> map = new HashMap<>();
        map.put(DsConstant.CODE, 1);
        String createUser = String.valueOf(request.getAttribute("createUser"));
        HelpCenter helpCenter = helpCenterService.getHelpCenterByHelpId(helpId);
        if (helpCenter == null) {
            map.put(DsConstant.MESSAGE, "帮助内容不存在");
            return map;
        }

        if (!StringUtils.isEmptyString(problem)) {
            helpCenter.setProblem(problem);
        }

        if (!StringUtils.isEmptyString(answer)) {
            helpCenter.setAnswer(answer);
        }

        if (sort != null) {
            helpCenter.setSort(sort);
        }

        if (status != null) {
            helpCenter.setStatus(status);
        }
        helpCenter.setModifyTime(new Date());
        helpCenter.setModifyUser(createUser);
        helpCenterService.modifyHelpCenter(helpCenter);
        map.put(DsConstant.CODE, DsConstant.CODE_SUCCESS);
        return map;
    }

    @ResponseBody
    @RequestMapping(value = "{helpId}/deleteHelpCenter", method = RequestMethod.POST)
    public Map<String, Object> deleteHelpCenter(@PathVariable("helpId") Long helpId) {
        Map<String, Object> map = new HashMap<>();
        map.put(DsConstant.CODE, 1);
        HelpCenter helpCenter = helpCenterService.getHelpCenterByHelpId(helpId);
        if (helpCenter == null) {
            map.put(DsConstant.MESSAGE, "帮助内容不存在");
            return map;
        }

        helpCenterService.deleteHelpCenter(helpCenter);
        map.put(DsConstant.CODE, DsConstant.CODE_SUCCESS);
        return map;
    }

    @ResponseBody
    @RequestMapping(value = "{helpId}/helpStatusChange", method = RequestMethod.POST)
    public Map<String, Object> helpStatusChange(@PathVariable("helpId") Long helpId, Integer status, String token) {
        Map<String, Object> map = new HashMap<>();
        map.put(DsConstant.CODE, 1);
        Long lognId = CommonUtils.getAdminIdByToken(token);
        if (lognId == 0) {
            map.put(DsConstant.MESSAGE, "token过期");
            return map;
        }
        HelpCenter helpCenter = helpCenterService.getHelpCenterByHelpId(helpId);
        if (helpCenter == null) {
            map.put(DsConstant.MESSAGE, "帮助内容不存在");
            return map;
        }

        helpCenter.setStatus(status);
        helpCenterService.modifyHelpCenter(helpCenter);
        map.put(DsConstant.CODE, DsConstant.CODE_SUCCESS);
        return map;
    }

    @ResponseBody
    @RequestMapping(value = "/helpCenterList", method = RequestMethod.GET)
    public Map<String, Object> helpCenterList(Integer pageIndex, String text, String token) {
        Map<String, Object> map = new HashMap<>();
        map.put(DsConstant.CODE, 1);
        Long lognId = CommonUtils.getAdminIdByToken(token);
        if (lognId == 0) {
            map.put(DsConstant.MESSAGE, "token过期");
            return map;
        }
        PageList<HelpCenter> helpCenters = helpCenterService.pageByParams(pageIndex, text);
        map.put("list", helpCenters.getList());
        map.put("page", helpCenters.getPageData());

        map.put("text", text);
        map.put(DsConstant.CODE, DsConstant.CODE_SUCCESS);
        return map;
    }

    /**
     * 新增标签
     *
     * @param name
     * @param status
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/add-mark", method = RequestMethod.POST)
    public Map<String, Object> addMark(String name, Integer status, HttpServletRequest request) {
        if (StringUtils.isEmptyString(name)) {
            return responseConditionError();
        }
        String createUser = String.valueOf(request.getAttribute("createUser"));
        Mark mark = new Mark();
        mark.setName(name);
        mark.setCreateUser(createUser);
        return markService.addMark(mark);
    }

    /**
     * 修改标签
     *
     * @param id
     * @param name
     * @param status
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/{markId}/modify-mark", method = RequestMethod.POST)
    public Map<String, Object> modifyMark(@PathVariable long id, String name, Integer status,
                                          HttpServletRequest request) {
        Map<String, Object> map = new HashMap<>();
        map.put(DsConstant.CODE, 1);
        String createUser = String.valueOf(request.getAttribute("createUser"));
        Mark mark = markService.getMarkByMarkId(id);
        if (mark == null) {
            map.put(DsConstant.MESSAGE, "标签不存在");
            return map;
        }

        if (!StringUtils.isEmptyString(name)) {
            Mark temp = markService.getMarkByNameNotMarkId(id, name);
            if (temp != null) {
                map.put(DsConstant.MESSAGE, "标签名已存在");
                map.put(DsConstant.CODE, 1);
                return map;
            }

            mark.setName(name);
        }

        if (status != null) {
            mark.setStatus(status);
        }
        mark.setModifyUser(createUser);
        mark.setModifyTime(new Date());
        markService.modifyMark(mark);
        map.put(DsConstant.CODE, DsConstant.CODE_SUCCESS);
        return map;
    }

    /**
     * 删除标签
     *
     * @param id
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "{markId}/delete-mark", method = RequestMethod.POST)
    public Map<String, Object> deleteMark(@PathVariable long id) {
        Map<String, Object> map = new HashMap<>();
        map.put(DsConstant.CODE, 1);
        Mark mark = markService.getMarkByMarkId(id);
        if (mark == null) {
            map.put(DsConstant.MESSAGE, "标签不存在");
            return map;
        }

        markService.deleteMark(mark);
        map.put(DsConstant.CODE, DsConstant.CODE_SUCCESS);
        return map;
    }

    /**
     * 未用
     *
     * @param markId
     * @param status
     * @param token
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "{markId}/markStatusChange", method = RequestMethod.POST)
    public Map<String, Object> markStatusChange(@PathVariable("markId") Long markId, Integer status, String token) {
        Map<String, Object> map = new HashMap<>();
        map.put(DsConstant.CODE, 1);
        Long lognId = CommonUtils.getAdminIdByToken(token);
        if (lognId == 0) {
            map.put(DsConstant.MESSAGE, "token过期");
            return map;
        }
        Mark mark = markService.getMarkByMarkId(markId);
        if (mark == null) {
            map.put(DsConstant.MESSAGE, "标签不存在");
            return map;
        }

        mark.setStatus(status);
        markService.modifyMark(mark);
        map.put(DsConstant.CODE, DsConstant.CODE_SUCCESS);
        return map;
    }

    /**
     * 标签列表
     *
     * @param name
     * @param token
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/markList", method = RequestMethod.GET)
    public Map<String, Object> markList(String name, String token) {
        Map<String, Object> map = new HashMap<>();
        map.put(DsConstant.CODE, 1);
        Long lognId = CommonUtils.getAdminIdByToken(token);
        if (lognId == 0) {
            map.put(DsConstant.MESSAGE, "token过期");
            return map;
        }
        List<Mark> marks = markService.getByParams(name);

        map.put("list", marks);
        map.put(DsConstant.CODE, DsConstant.CODE_SUCCESS);

        return map;
    }

    /**
     * 新增轮播图
     *
     * @param title
     * @param imageUrl
     * @param linkUrl
     * @param bannerType
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/addBanner", method = RequestMethod.POST)
    @SystemLog(function = "addBanner", description = "添加轮播图")
    public Map<String, Object> addBanner(String title, @RequestParam("imageUrl") MultipartFile imageUrl, String linkUrl,
                                         Integer bannerType, HttpServletRequest request) {
        Map<String, Object> map = new HashMap<>();
        String createUser = String.valueOf(request.getAttribute("createUser"));
        if (imageUrl == null) {
            map.put(DsConstant.CODE, DsConstant.CODE_PICTURE_NOT_NULL);
            map.put(DsConstant.MESSAGE, DsConstant.MESSAGE_PICTURE_NOT_NULL);
            return map;
        }
        String path = FileUtils.saveBannerPicture(imageUrl);
        if ("".equals(path)) {
            map.put(DsConstant.CODE, DsConstant.CODE_PICTURE_UPLOAD_ERROR);
            map.put(DsConstant.MESSAGE, DsConstant.MESSAGE_PICTURE_UPLOAD_ERROR);
            return map;
        }

        Banner banner = new Banner(title, path, linkUrl, bannerType, createUser);

        bannerService.addBanner(banner);
        map.put(DsConstant.CODE, DsConstant.CODE_SUCCESS);
        return map;
    }

    /**
     * 修改轮播图
     *
     * @param bannerId
     * @param title
     * @param imageUrl
     * @param linkUrl
     * @param bannerType
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/{bannerId}/modifyBanner", method = RequestMethod.POST)
    @SystemLog(function = "modifyBanner", description = "修改轮播图信息")
    public Map<String, Object> modifyBanner(@PathVariable("bannerId") Long bannerId, String title,
                                            @RequestParam(value = "imageUrl", required = false) MultipartFile imageUrl, String linkUrl, Integer bannerType,
                                            HttpServletRequest request) {
        Map<String, Object> map = new HashMap<>();
        String createUser = String.valueOf(request.getAttribute("createUser"));
        Banner banner = bannerService.getById(bannerId);
        if (banner == null) {
            map.put(DsConstant.CODE, DsConstant.CODE_BANNER_LOST);
            map.put(DsConstant.MESSAGE, DsConstant.MESSAGE_BANNER_LOST);
            return map;
        }

        if (!StringUtils.isEmptyString(title)) {
            banner.setTitle(title);
        }

        if (StringUtils.isEmptyString(linkUrl)) {
            banner.setLinkUrl(linkUrl);
        }

        if (bannerType != null) {
            banner.setBannerType(bannerType);
        }

        if (imageUrl != null) {
            String path = FileUtils.saveBannerPicture(imageUrl);
            banner.setImageUrl(path);
        }

        banner.setModifyUser(createUser);
        banner.setModifyTime(new Date());

        bannerService.modifyBanner(banner);
        map.put(DsConstant.CODE, DsConstant.CODE_SUCCESS);
        return map;
    }

    /**
     * 删除轮播图
     *
     * @param bannerId
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "{bannerId}/deleteBanner", method = RequestMethod.POST)
    @SystemLog(function = "deleteBanner", description = "删除轮播图")
    public Map<String, Object> deleteBanner(@PathVariable("bannerId") Long bannerId) {
        Map<String, Object> map = new HashMap<>();
        Banner banner = bannerService.getById(bannerId);
        if (banner == null) {
            map.put(DsConstant.CODE, DsConstant.CODE_BANNER_LOST);
            map.put(DsConstant.MESSAGE, DsConstant.MESSAGE_BANNER_LOST);
            return map;
        }

        bannerService.deleteBanner(banner);
        map.put(DsConstant.CODE, DsConstant.CODE_SUCCESS);
        return map;
    }

    /**
     * 轮播图列表
     *
     * @param title
     * @param bannerType
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/bannerList", method = RequestMethod.GET)
    public Map<String, Object> bannerList(String title, Integer bannerType) {
        Map<String, Object> map = new HashMap<>();
        List<Banner> banners = bannerService.getByParams(title, bannerType);

        map.put("list", banners);
        map.put(DsConstant.CODE, DsConstant.CODE_SUCCESS);
        return map;
    }

    /**
     * 轮播图详情
     *
     * @param bannerId
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/{bannerId}/bannerDetail", method = RequestMethod.GET)
    public Map<String, Object> bannerDetail(@PathVariable("bannerId") Long bannerId) {

        Map<String, Object> map = new HashMap<>();
        Banner banner = bannerService.getById(bannerId);
        map.put("obj", banner);
        map.put(DsConstant.CODE, DsConstant.CODE_SUCCESS);
        return map;
    }

    @ResponseBody
    @RequestMapping(value = "/teacher", method = RequestMethod.POST)
    public Map<String, Object> addTeacher(String name, @RequestParam("headImg") MultipartFile headImg, String introduction, String marks, HttpServletRequest request) {
        Map<String, Object> map = new HashMap<>();
        map.put(DsConstant.CODE, 1);
        String createUser = String.valueOf(request.getAttribute("createUser"));
        String pictureStr = FileUtils.saveBannerPicture(headImg);
        if ("".equals(pictureStr)) {
            map.put(DsConstant.CODE, DsConstant.CODE_PICTURE_NOT_NULL);
            map.put(DsConstant.MESSAGE, DsConstant.MESSAGE_PICTURE_NOT_NULL);
            return map;
        }
        Teacher teacher = new Teacher(name, pictureStr, introduction, marks, createUser);
        teacherService.addTeacher(teacher);
        map.put(DsConstant.CODE, DsConstant.CODE_SUCCESS);
        return map;
    }

    @ResponseBody
    @RequestMapping(value = "/{teacherId}/teacher", method = RequestMethod.POST)
    public Map<String, Object> modifyTeacher(@PathVariable("teacherId") Long teacherId, String name,
                                             String introduction, String marks, HttpServletRequest request) {

        Map<String, Object> map = new HashMap<>();
        map.put(DsConstant.CODE, 1);
        String createUser = String.valueOf(request.getAttribute("createUser"));
        Teacher teacher = teacherService.getTeacherByTeacherId(teacherId);
        if (teacher == null) {
            map.put(DsConstant.MESSAGE, "讲师不存在");
            map.put(DsConstant.CODE, 1);
            return map;
        }
        if (!StringUtils.isEmptyString(name)) {
            teacher.setName(name);
        }

        if (!StringUtils.isEmptyString(introduction)) {
            teacher.setIntroduction(introduction);
        }

        if (!StringUtils.isEmptyString(marks)) {
            teacher.setMarks(marks);
        }
        teacher.setModifyTime(new Date());
        teacher.setModifyUser(createUser);
        teacherService.updateTeacher(teacher);
        map.put(DsConstant.CODE, DsConstant.CODE_SUCCESS);
        return map;
    }

    @ResponseBody
    @RequestMapping(value = "/teacher", method = RequestMethod.GET)
    public Map<String, Object> teacherList(Integer pageIndex, Integer pageSize, String name, String token) {

        Map<String, Object> map = new HashMap<>();
        map.put(DsConstant.CODE, 1);
        Long adminId = CommonUtils.getAdminIdByToken(token);
        if (adminId == 0) {
            map.put(DsConstant.MESSAGE, "token过期");
            return map;
        }
        PageList<Teacher> userPage = teacherService.pageByParams(pageIndex, pageSize == null ? DsConstant.PAGESIZE_DEFAULT : pageSize, name);
        map.put("page", userPage.getPageData());
        map.put("list", userPage.getList());
        map.put(DsConstant.CODE, DsConstant.CODE_SUCCESS);

        return map;
    }

    @ResponseBody
    @RequestMapping(value = "/teacherSearch", method = RequestMethod.GET)
    public Map<String, Object> teacherSearch(String name) {
        Map<String, Object> map = new HashMap<>();
        map.put(DsConstant.CODE, DsConstant.CODE_SUCCESS);
        List<Teacher> users = teacherService.getTeacherByNameF(name);
        map.put("list", users);
        map.put(DsConstant.CODE, DsConstant.CODE_SUCCESS);

        return map;
    }

    /**
     * 日志列表
     * @param pageIndex
     * @param username
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/logs", method = RequestMethod.GET)
    public Map<String, Object> logs(int pageIndex, int pageSize, String username) {
        pageIndex = setPageIndex(pageIndex);
        pageSize = setPageSize(pageSize, DsConstant.PAGESIZE_DEFAULT);
        return logService.page(pageIndex, pageSize, username);
    }

}
